var SychiInitFlag = false;
var SysjmInitFlag = false;
var SychiLoginFlag = false;
let TkxInitFlag = false;

function plugin() {
    return document.getElementById('plugin');
}


//职工医保初始化
function SychiInitDll() {
    plugin().initWindow("SYJinBaoMedicare");
    SychiInit();
}

function SychiInit() {
    var strJson = {action: "SY_JINBAO_INIT", args: ""};
    var result = plugin().syncExecute(JSON.stringify(strJson));
    var jsonObj = JSON.parse(JSON.parse(result).args);
    //console.info(jsonObj);
    if (jsonObj.resultSet[0].result != 'ok') {
        SychiInitFlag = false;
    } else {
        SychiInitFlag = true;
    }
    return SychiInitFlag;
}

//签到
function func9100(hospCode) {
    if (!SychiLoginFlag) {
        var flag = false;
        var timestamp = new Date().getTime();
        var argJson = {
            params: {filePath: ""},
            commandSet: [{
                jybh: "",
                hospCode: hospCode,
                jysrParam: "9100^" + hospCode + "^99999^^" + formatDate(timestamp) + "^0000^" + "|^",
                jysrParam2: "",
                isSendBackground: "0",
                areaCode: hospCode
            }]
        };
        var strJson = {action: "SY_JINBAO_CALL", args: JSON.stringify(argJson)};
        var result = plugin().syncExecute(JSON.stringify(strJson));
        var jsonObj = JSON.parse(JSON.parse(result).args);
        if (jsonObj.resultSet[0].result == "ok") {
            SychiLoginFlag = true;
        }
    }
    return SychiLoginFlag;

}

//签退
function func9110(hospCode) {
    var timestamp = new Date().getTime();
    var argJson = {
        params: {filePath: ""},
        commandSet: [{
            jybh: "",
            hospCode: hospCode,
            jysrParam: "9110^" + hospCode + "^99999^^" + formatDate(timestamp) + "^0000^" + "|^",
            jysrParam2: "",
            isSendBackground: "0",
            areaCode: hospCode
        }]
    };
    var strJson = {action: "SY_JINBAO_CALL", args: JSON.stringify(argJson)};
    var result = plugin().syncExecute(JSON.stringify(strJson));
    var jsonObj = JSON.parse(JSON.parse(result).args);
    if (jsonObj.resultSet[0].result != "ok") {
        console.warn("签退失败," + jsonObj);
    } else {
        //console.info("签退成功...");
    }
    return JSON.parse(result).args;
}

//职工医保交易
function SychiCall(jybh, hospCode, jysrParam) {

    SychiInitDll();

    if (SychiInitFlag) {
        var result = func9100(hospCode);

        if (!result) {
            console.warn("签到失败," + result);
            return result;
        } else {
            //console.info("签到成功...");
            var timestamp = new Date().getTime();
            var argJson = {
                params: {filePath: ""},
                commandSet: [{
                    jybh: jybh,
                    hospCode: hospCode,
                    jysrParam: jybh + "^" + hospCode + "^99999^^" + formatDate(timestamp) + "^0000^" + jysrParam + "|^",
                    jysrParam2: "",
                    isSendBackground: "0",
                    areaCode: hospCode
                }]
            };
            var strJson = {action: "SY_JINBAO_CALL", args: JSON.stringify(argJson)};
            var result = plugin().syncExecute(JSON.stringify(strJson));
            console.warn("<" + new Date().toLocaleString() + ">\n" + JSON.stringify(strJson) + "\n" + result);
            // func9110(hospCode);
            return JSON.parse(result).args;
        }
    } else {
        return false;
    }

}

//居民医保初始化
function SysjmInitDll() {
    plugin().initWindow("SYPMedicare");
    SysjmInit();
}

//居民医保初始化
function SysjmInit() {
    var strJson = {action: "SYPMEDICARE_INIT", args: ""};
    var result = plugin().syncExecute(JSON.stringify(strJson));
    var jsonObj = JSON.parse(JSON.parse(result).args);
    //console.info(jsonObj);
    if (jsonObj.resultSet[0].result != 'ok') {
        SysjmInitFlag = false;
    } else {
        SysjmInitFlag = true;
    }
}

//居民医保调用交易主函数
function SysjmCall(Jybh, JysrXml) {
    SysjmInitDll();
    if (SysjmInitFlag) {
        var argJson = {commandSet: [{Jybh: Jybh, JysrXml: JysrXml}]};
        var strJson = {action: "SYPMEDICARE_CALL", args: JSON.stringify(argJson)};
        var result = plugin().syncExecute(JSON.stringify(strJson));
        return JSON.parse(result).args;
    }
}

//托克逊医保初始化
function InitTKXMedicareDll() {
    var result = plugin().initWindow("TKXCMedicare");
    if (result) {
        return InitTKXMedicareInterface();
    }
}

//初始化
function InitTKXMedicareInterface() {
    var strJson = {action: "TXK_CMEDICARE_INIT", args: ""};
    var result = plugin().syncExecute(JSON.stringify(strJson));
    var jsonObj = JSON.parse(JSON.parse(result).args);
    //console.info(jsonObj);
    if (jsonObj.resultSet[0].result != 'ok') {
        TkxInitFlag = false;
    } else {
        TkxInitFlag = true;
    }
    return jsonObj;
}

//托克逊医保调用交易主函数
function TkxCall(Jybh, JysrXml) {
    let result = InitTKXMedicareDll();
    if (TkxInitFlag) {
        var argJson = {commandSet: [{Jybh: Jybh, JysrXml: JysrXml}]};
        var strJson = {action: "TXK_CMEDICARE_CALL", args: JSON.stringify(argJson)};
        result = plugin().syncExecute(JSON.stringify(strJson));
        return JSON.parse(result).args;
    } else {
        return "医保初始化失败," + JSON.stringify(result);
    }
}

//时间格式转换
function formatDate(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return "" + y + m + d + h + minute + second;
}











